package in.muton.edushare.components.messaging.recievepanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.messaging.util.MessageTypeParser;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.panels.images.ImageWithLink;
import in.muton.edushare.poll.pages.PollAnswer;
import in.muton.edushare.schedules.widgets.DateLabel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.PollsDAO;
import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.UserPoll;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class PollMessagePanel extends EdusharePanel {
	private static final long serialVersionUID = 4048415545646358401L;

	public PollMessagePanel(String id, IModel<Messages> model) {
		super(id, model);
		this.model = model;
		init();
		IFileUploadable entity = MessageTypeParser.getObject(model.getObject()
				.getReceiverId(), model.getObject().getReceiverType());
		add(new ImageWithLink("senderImage", new CayenneModel<IFileUploadable>(
				entity), ApplicationConstants.FILE_SUFFIX_ICON));

		WebMarkupContainer wmc = new WebMarkupContainer("background");
		add(wmc);
		wmc.add(MessageTypeParser.getURLPanel(
				model.getObject().getReceiverId(), model.getObject()
						.getReceiverType(), "name"));
		wmc.add(new Label("msg", message).setEscapeModelStrings(false));
		add(new DateLabel("startTime",
				pollModel.getObject().getPollStartDate(), 0));
		add(new DateLabel("endTime", pollModel.getObject().getPollEndDate(), 0));
		add(new EduShareURLPanel("creator", new CayenneModel<Users>(pollModel
				.getObject().getCreator())));
		PageParameters params = new PageParameters();
		params.put("0", pollModel.getObject().getPollId().toString());
		add(new BookmarkablePageLink<Void>("vote", PollAnswer.class, params){
			private static final long serialVersionUID = 1704055433090802096L;
			@Override
			public boolean isVisible(){
				Users u = getLoggedInUser();
				Poll p  = pollModel.getObject();
				UserPollDAO upd = new UserPollDAO(getORMInterface());
				if(upd.getPollStatus(u, p) == UserPoll.POLL_COMPLETE)
					return false;
				return true;
			}
		});
	}

	private void init() {
		Poll p = getPoll(model.getObject().getMessage());
		pollModel = new CayenneModel<Poll>(p);
		message = "Your feedback is requested for the poll " + p.getPollName();
	}

	public static boolean exists(String str){
		if(getPoll(str) != null)
			return true;
		return false;
	}
	
	public static Poll getPoll(String str){
		ORMInterface orm = EduShareSession.get().getSessionORMInterface();
		int startIndex = ("<<<EDUSHAREMESSAGINGMETADATA:POLL:").length();
		int endIndex = str.indexOf(">", startIndex);
		String pollId = str.substring(startIndex, endIndex);
		Poll p = new PollsDAO(orm).getPollById(Integer.parseInt(pollId));
		return p;
	}
	
	private IModel<Messages> model = null;
	private IModel<Poll> pollModel = null;
	private String message = "";
}
